The func command defines a function. A function is distinct from a procedure from the fact that a function must return a value whereas a procedure must not. Arguments are given in the definition with any name prototype representative of the data type. As in C, the argument list must be comma separated when calling the function (after having defined it). An example follows. func is a C-calculator mode command.
The prototype list defines the type of variable to be used. Although all global variables are accessible from within the function, variables are always searched for from the prototype list first, then from the local list (auto variables), and finally from the global list. All scalar variables are passed by value: thus any scalar expression is legal as scalar argument. String arguments and vector arguments are passed by pointer: thus string and vector arguments must refer to a variable explicitly. The show table can be used to list all the installed objects at a given time.
func functionname(proto-list) cmode-line-statement; return(value)
or
func functionname(proto-list) {
cmode-statements
return(value)
}
# The following example will print the factorial of all integers up to 120. cmode func fac(x) { # This `x' is a prototype: it does not exist. if (x <= 0) { return(1) } else { return(x * fac(--x)) } } x=1 # This `x' is a global scalar variable while (x<120) { fac(x++) } fmode # The following calculates the average of a vector cmode func avg(X) { auto i,x for (x=0,i=1;i<=data;i++) { x += X[i] } return(x/data) } fmode
C, return, for, while, cmode, math, free, proc, show table, install